<template>
  <div>
    <el-form ref="activityRefForm" :model="activityQuery" label-width="110px" :rules="activityRules">
      <el-form-item label="负责人" prop="ownerId">
        <el-select v-model="activityQuery.ownerId" filterable placeholder="请选择" class="width">
          <el-option
            v-for="item in ownerOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="活动名称" prop="name">
        <el-input v-model="activityQuery.name" />
      </el-form-item>
      <el-form-item label="开始时间" prop="startTime">
        <el-date-picker
          v-model="activityQuery.startTime"
          size="small"
          type="datetime"
          placeholder="请选择创建时间"
          style="width: 100%"
          value-format="YYYY-MM-DD HH:mm:ss"
        />
      </el-form-item>
      <el-form-item label="结束时间" prop="endTime" >
        <el-date-picker
          v-model="activityQuery.endTime"
          size="small"
          type="datetime"
          style="width: 100%"
          placeholder="请选择创建时间"
          value-format="YYYY-MM-DD HH:mm:ss"
        />
      </el-form-item>
      <el-form-item label="活动预算" prop="cost">
        <el-input v-model="activityQuery.cost" />
      </el-form-item>
      <el-form-item label="活动描述" prop="description">
        <el-input type="textarea" :rows="6" v-model="activityQuery.description" placeholder="请输入活动描述" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="activitySubmit">提 交</el-button>
        <el-button @click="goback">返 回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { doGet, doPost, doPut } from '../http/httpRequest'
import { messageTip } from '../util/util'

export default {
  name: 'ActivityRecordView',
  inject: ['reload'],
  data() {
    return {
      // 市场活动表单独享
      activityQuery: {},
      // 市场活动表单验证规则
      activityRules: {
        ownerId: [
          { required: true, message: '负责人不能为空', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '活动名称不能为空', trigger: 'blur' }
        ],
        startTime: [
          { required: true, message: '开始时间不能为空', trigger: 'blur' }
        ],
        endTime: [
          { required: true, message: '结束时间不能为空', trigger: 'blur' }
        ],
        cost: [
          { required: true, message: '活动预算不能为空', trigger: 'blur' },
          { pattern: /^[0-9]+(\.[0-9]{2})?$/, message: '活动预算必须是整数或两位小数', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '活动描述不能为空', trigger: 'blur' },
          { min: 5, max: 255, message: '活动描述长度为5~255个字符', trigger: 'blur' }
        ]
      },
      // 负责人的下拉选项
      ownerOptions: [{}]
    }
  },
  mounted() {
    this.loadOwner()
    this.loadActivity()
  },
  methods: {
    // 返回
    goback() {
      this.$router.go(-1)
    },
    // 加载负责人
    loadOwner() {
      doGet('/api/owner', {}).then(resp => {
        if (resp.data.code === 200) {
          this.ownerOptions = resp.data.data
        }
      })
    },
    // 市场活动提交
    activitySubmit() {
      let formData = new FormData()
      for (let field in this.activityQuery) {
        if(this.activityQuery[field]) {
          formData.append(field, this.activityQuery[field])
        }
      }
      this.$refs.activityRefForm.validate(isValid => {
        if (isValid) {
          if (this.activityQuery.id > 0) {
            // 编辑
            doPut('/api/activity', formData).then(resp => {
              console.log(resp);
              if(resp.data.code === 200) {
                messageTip('编辑成功', 'success')
                this.$router.push('/dashboard/activity')
              } else {
                messageTip('编辑失败', 'error')
              }
            })
          } else {
            // 新增
            doPost('/api/activity', formData).then(resp => {
              if (resp.data.code === 200) {
                messageTip('提交成功', 'success')
                // 跳转到市场活动列表页
                this.$router.push('/dashboard/activity')
              } else {
                messageTip('提交失败', 'error')
              }
            })
          }
        }
      })
    },
    // 加载要编辑的市场活动数据
    loadActivity() {
      let id = this.$route.params.id
      // id存在, id不为空, 说明是编辑
      if (id) {
        doGet('/api/activity/' + id, {}).then(resp => {
          if (resp.data.code === 200) {
            this.activityQuery = resp.data.data
          }
        })
      }
    }
  }
}
</script>

<style scoped>
  .el-form-item {
    margin-bottom: 20px;
  }
</style>
<style>
  .width {
    width: 100%
  }
</style>